﻿@using Himall.Model
@using Himall.Core
@{
    ViewBag.Title = "MenuManage";
}

<script type="text/javascript" src="/Areas/Admin/Scripts/weixinMenuAdd.js"></script>

@model List<Himall.Web.Areas.Admin.Models.MenuManageModel>

<form>
    <div class="container">
        <ul class="nav nav-tabs-custom clearfix">
            <strong>菜单设置</strong>
            <li class="active"><a>菜单管理</a></li>
            <li><a onclick="DialogAddMainMenu()">添加一级菜单</a></li>
            <li><a onclick="DialogAddMenu()">添加二级菜单</a></li>
        </ul>
        <table class="table mt20">
            <thead>
                <tr>
                    <th>一级菜单名称</th>
                    <th>二级菜单名称</th>
                    <th>跳转连接</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @item.TopMenuName
                        </td>
                        <td></td>
                        <td>@(item.LinkType.ToDescription())&nbsp;&nbsp;&nbsp;&nbsp;@item.URL</td>
                        <td>
                            <span class="btn-a">
                                <a class="good-check" href="EditMenu?menuId=@item.ID">编辑</a> <a class="good-check" onclick="DeleteMainMenu(@item.ID)">删除</a>
                            </span>
                        </td>
                    </tr>
                    foreach (var menu in item.SubMenu)
                    {

                        <tr>
                            <td></td>
                            <td>@menu.Title</td>
                            @if (!string.IsNullOrWhiteSpace(menu.Url))
                            {
                                <td>@menu.Url</td>
                            }
                            else
                            {
                                <td>@menu.UrlType.ToDescription()</td>

                            }
                            <td>
                                <span class="btn-a">
                                    <a class="good-check" href="EditMenu?menuId=@menu.Id">编辑</a> <a class="good-check" onclick="DeleteMenu(@menu.Id)">删除</a>
                                </span>
                            </td>
                        </tr>
                    }
                }
            </tbody>
        </table>

        <div class="table-bt clearfix">
            <div class="col-sm-10">
                <button type="button" class="btn btn-primary" onclick="RequestToWeixin()">同步至微信</button>
            </div>
        </div>
        <script>
            function DeleteMainMenu(MenuId) {
                $.dialog.confirm('删除该分类将会同时删除该分类的所有下级分类，您确定要删除吗？', function ()
                {
                    var loading = showLoading();
                    $.post('./DeleteMenu', { menuId: MenuId }, function (result) {
                        loading.close();
                        if (result.success) {
                            $.dialog.tips('删除成功');
                            location.reload();
                        }
                        else
                            $.dialog.alert('删除失败！' + result.msg);
                    });
                });
            }
            function DeleteMenu(MenuId){
                //var loading = showLoading();
                $.dialog.confirm('确定删除该分类吗？', function () {
                    var loading = showLoading();
                    $.post('./DeleteMenu', { menuId: MenuId }, function (result) {
                        loading.close();
                        if (result.success) {
                            $.dialog.tips('删除成功');
                            location.reload();
                        }
                        else
                            $.dialog.alert('删除失败！' + result.msg);
                    });
                });
            }

            function DialogAddMenu() {
                var title=$("#txtMenuName").val();
                if(title,length>7)
                    alert("二级菜单限7个字符");
                var urlType=$("#ddlType").val();
                var url=$("#menuUrl").val();
                var parentId=$("#mainMenu").val();
                AddWeixinMenu("childMenu");
            }

            function DialogAddMainMenu() {
                var title = $("#txtMenuName1").val();
                if(title.length>5)
                    alert("一级菜单限5个字符")
                var urlType = $("#ddlType1").val();
                var url = $("#menuUrl1").val();
                AddMainMenu("mainMenu");
            }

            function RequestToWeixin()
            {
                var loading = showLoading();
                $.post('./RequestToWeixin', { }, function (result) {
                    loading.close();
                    if (result.success) {
                        $.dialog.tips('同步成功',function(){
                            location.reload();
                        });
                        
                    }
                    else
                        $.dialog.alert('同步失败！' + result.msg);
                });
            }

        </script>
    </div>
    <div id="mainMenu" style="display:none">
        <div class="dialog-form">
            <div class="form-group">
                <label for="" class="label-inline">菜单名称：</label>
                <input type="text" class="form-control input-sm" id="txtMenuName1">
                <p class="help-block">限5个字符</p>
            </div>
            <div class="form-group">
                <label for="" class="label-inline">链接对象：</label>
                <select id="ddlType1" onchange="Onchange($(this))" class="form-control input-sm">
                    <option value="0">不绑定</option>
                    <option value="1">首页</option>
                    <option value="2">微店</option>
                    <option value="3">商城分类</option>
                    <option value="4">个人中心</option>
                    <option value="5">购物车</option>
                    <option value="6">链接</option>
                </select>
                <p class="help-block">选择链接对象后将不能添加二级菜单</p>
            </div>
            <div class="form-group linkUrl" style="display:none">
                <label for="" class="label-inline">链接地址：</label>
                <input type="text" class="form-control input-sm" id="menuUrl1" />
            </div>
        </div>
    </div>
    <div id="childMenu" style="display:none;">
        <div class="dialog-form">
            <div class="form-group">
                <label for="" class="label-inline">菜单名称：</label>
                <input type="text" class="form-control input-sm" id="txtMenuName">
                <p class="help-block">限7个字符</p>
            </div>
            <div class="form-group">
                <label for="" class="label-inline">上级菜单：</label>
                <select id="mainMenu" class="form-control input-sm">
                    @foreach (var item in Model.Where(i => i.LinkType == Himall.Model.MenuInfo.UrlTypes.Nothing))
                    {
                        <option value=@item.ID>@item.TopMenuName</option>
                    }
                </select>
            </div>
            <div class="form-group">
                <label for="" class="label-inline">链接对象：</label>
                <select id="ddlType" onchange="Onchange($(this))" class="form-control input-sm">
                    <option value="1">首页</option>
                    <option value="2">微店</option>
                    <option value="3">商城分类</option>
                    <option value="4">个人中心</option>
                    <option value="5">购物车</option>
                    <option value="6">链接</option>
                </select>
            </div>
            <div class="form-group linkUrl" style="display:none">
                <label for="" class="label-inline">链接地址：</label>
                <input type="text" class="form-control input-sm" id="menuUrl" value="http://" />
            </div>
        </div>
    </div>

    <script>
    function Onchange(obj) {
        if (obj.val() == "6")
        {
            obj.parents('.dialog-form').find(".linkUrl").show();
            obj.parents('.dialog-form').find(".linkUrl").find('input').eq(0).val();
        }
        else {
            obj.parents('.dialog-form').find(".linkUrl").hide();
            obj.parents('.dialog-form').find(".linkUrl").find('input').eq(0).val();
        }
    }

    </script>
</form>


